package com.spps;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Template {
    public static String newContent;

    public static String compile(String content, HashMap<String, String> dataSource) {
        newContent = content;
        dataSource.forEach((key, value) -> {
            // 替换带有修饰符的
            Pattern pattern1 = Pattern.compile("%" + key + "\\|capitalizes%");
            Matcher matcher1 = pattern1.matcher(newContent);
            newContent = matcher1.replaceAll(StringUtils.capitalizes(value));

            // 替换没有修饰符的
            Pattern pattern2 = Pattern.compile("%" + key + "%");
            Matcher matcher2 = pattern2.matcher(newContent);
            newContent = matcher2.replaceAll(value);
        });
        return newContent;
    }

    // public static String compileForeach(String content, ArrayList<HashMap<String,
    // String>> dataSource) {
    // Pattern pattern = Pattern.compile("%foreach%(.*?)%endforeach%");
    // Matcher matcher = pattern.matcher(content);
    // String newContent = "";
    // if (matcher.find()) {
    // String template = matcher.group(1);
    // for (int i = 0; i < dataSource.size(); i++) {
    // newContent += template + dataSource.get(i).get("name");
    // }
    // return newContent;
    // }
    // return "";
    // }
}
